<?php


namespace App\Helper\Utils;

/**
 * Class TimeUtil
 * @package App\Helper\Utils
 */
class TimeUtil
{
    /**
     * 当前毫秒时间
     *
     * @return float
     */
    public static function getMillisecond()
    {
        return round(microtime(true) * 1000);
    }

    /**
     * 是否是周五
     *
     * @return bool
     */
    public static function isFriday()
    {
        $nowTime = time();
        $thisWeekFriday = strtotime("friday this week");
        return ($nowTime > $thisWeekFriday && $nowTime < $thisWeekFriday + 86400) ? true : false;
    }

    /**
     * 时间区间
     * @param string $sign
     * @return array
     */
    public static function convertDateSignToUnix($sign = 'today'): array
    {
        switch ($sign) {
            case "today"://今天
                $startDate = mktime(0, 0, 0);
                $endDate = mktime(23, 59, 59);
                break;
            case "yesterday"://昨天
                $startDate = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
                $endDate = mktime(23, 59, 59, date('m'), date('d') - 1, date('Y'));
                break;
            case "last30"://最近30天
                $startDate = mktime(0, 0, 0, date('m'), date('d') - 30, date('Y'));
                $endDate = mktime(23, 59, 59, date('m'), date('d') - 1, date('Y'));
                break;
            case "lastWeek"://上周
                $startDate = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - 7, date('Y'));
                $endDate = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - 7, date('Y'));
                break;
            case "lastMonth"://上月
                $startDate = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
                $endDate = mktime(23, 59, 59, date('m'), 1, date('Y')) - 24 * 3600;
                break;
            case "month"://本月
                $startDate = mktime(0, 0, 0, date('m'), 1, date('Y'));
                $endDate = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
                break;
            case "lastTwoWeek"://最近2周
                $startDate = mktime(0, 0, 0, date('m'), date('d') - 14, date('Y'));
                $endDate = mktime(23, 59, 59, date('m'), date('d') - 1, date('Y'));
                break;
            default://默认今天
                $startDate = mktime(0, 0, 0);
                $endDate = mktime(23, 59, 59);
                break;
        }
        return [$startDate, $endDate];
    }

    /**
     * 获取日期区间
     * @param $first string 开始时间(2019-07-01)
     * @param $last string 结束时间(2019-07-10)
     * @param string $step
     * @param string $format
     * @return array
     */
    public static function dateRange($first, $last, $format = 'Y-m-d', $step = '+1 day')
    {
        $dates = [];
        $current = strtotime($first);
        $last = strtotime($last);

        while ($current <= $last) {
            $dates[] = date($format, $current);
            $current = strtotime($step, $current);
        }

        return $dates;
    }
    /**
     * 格式化时间格式
     * @param int|string $sourceTime 需要格式化的时间
     * @return string
     */
    public static function formatDateStyle($sourceTime)
    {
        if (!$sourceTime) {
            return '';
        }
        if (!is_numeric($sourceTime)) {
            $sourceTime = strtotime($sourceTime);
        }
        $currentTime = time();

        $diffTime = $currentTime - $sourceTime;
        if (date('Ymd', strtotime('-1 day', $currentTime)) == date('Ymd', $sourceTime)) {
            return '昨天';
        } elseif ($diffTime < 60) {
            return '刚刚';
        } elseif ($diffTime < 3600) {
            return intval($diffTime / 60) . '分钟前';
        } elseif ($diffTime < 3600 * 24) {
            return intval($diffTime / 3600) . '小时前';
        } elseif ($diffTime < 31536000) {
            return ceil($diffTime / 86400) . '天前';
        } elseif ($diffTime >= 31536000) {
            return intval($diffTime / 31536000) . '年前';
        } else {
            return '';
        }
    }
}
